import Dexie, { Table } from 'dexie';
import dicts, { Dict } from './initData/dicts';
import users, { User } from './initData/users';
import menus, { Menu } from './initData/menus';
import roles, { Role } from './initData/roles';
import { Token } from './initData/tokens';

export class MySubClassedDexie extends Dexie {
  users!: Table<User>;
  roles!: Table<Role>;
  menus!: Table<Menu>;
  tokens!: Table<Token>;
  dicts!: Table<Dict>;

  constructor() {
    super('myDatabase');
    this.version(1).stores({
      // 定义表的主键与索引，索引可以提高检索性能。
      users: '++uid, &name, status, [name+password]',
      roles: 'key, status, *menus, [key+status]',
      menus: 'key, status, parent, [parent+key]',
      tokens: '++id, token, uid, status',
      dicts: 'key, parent, status, [parent+key]',
    });
  }
}
const db = new MySubClassedDexie();
db.on('populate', async () => {
  // 并发
  Promise.all([
    db.users.bulkAdd(users),
    db.roles.bulkAdd(roles),
    db.menus.bulkAdd(menus),
    db.dicts.bulkAdd(dicts),
  ]);
});

export default db;
